package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 员工类的service接口的实现类
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    @Transactional
    public void save(Employee employee) {
        employeeMapper.save(employee);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public Employee selectById(Long id) {
        return (Employee) employeeMapper.selectById(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }
    //高级查询和分页
    @Override
    public PageBean<Employee> queryPage(BaseQuery baseQuery) {
        Long conut = employeeMapper.getConut(baseQuery);
        if (conut==0||conut<=0){
            return new PageBean<>();
        }else {
            List<Employee> list = employeeMapper.queryPage(baseQuery);
            return new PageBean<Employee>(conut,list);
        }
    }


    /**
     * 员工登陆
     * @param username
     * @param password
     * @return
     */
    @Override
    public Employee findByUsernameAndPassword(String username, String password) throws CustomException {
        // 通过用户名查询员工对象
        Employee employee = employeeMapper.loadByUsername(username);
        // 判断员工对象是否为空
        if (employee == null) {
            // 抛出异常
            throw new CustomException("用户名错误，请重新填写！");
        }
        // 用户名不为空，判断密码是否正确，如果不正确，抛出异常
        if (!employee.getPassword().equals(password)) {
            throw new CustomException("密码错误，请重新填写！");
        }
        return employee;
    }


}
